// 引入数据库连接
let connection = require('../app/database.js')
// 用户service类
class UserService{
    // 根据用户名查询用户
    async GetUserByName(name){
        // 创建查询语句
        let statement = `SELECT * FROM user WHERE name=?;`
        let result = await connection.execute(statement,[name]);
        return result[0]
    }
    // 更新用户在线state信息
    async UpdateUserStateService(id,state){
        console.log('userID---',id,typeof id);
        let statement = `update user set state = ? where id = ?`
        let result = await connection.execute(statement,[state,id]);
        return result[0].affectedRows
    }
    // 获取在线用户列表
    async GetUserListService(){
       // let statement = `SELECT * FROM user WHERE state=1`;
        // 获取所有用户-包括在线和不在线
        let statement = `SELECT * FROM user`;
        let result = await connection.execute(statement);
        return result[0];
    }
    // 用户自定义图片上传
    async UpdateUserConfigService({type,imgUrl,userID}){
        // 存储用户头像服务器路径
        let statement = '';
        switch (type){
            case 'chartBgUrl': statement = `UPDATE user SET chartBgUrl = ? WHERE id = ?;`;break;
            case 'avatarUrl': statement = `UPDATE user SET avatarUrl = ? WHERE id = ?;`;break;
        }
        let result = await connection.execute(statement,[imgUrl,userID]);
        return result[0].affectedRows
    }
    // 获取用户自定义配置》自定义头像/自定义聊天背景
    async GetUserConfigService(type,userID){
        // 根据用户id获取用户头像信息
        let statement = ``;
        switch (type){
            case 'chartBgUrl': statement = `SELECT chartBgUrl FROM user WHERE id = ?`;break;
            case 'avatarUrl': statement = `SELECT avatarUrl FROM user WHERE id = ?`;break;
        }
        let result = await connection.execute(statement,[userID]);
        console.log('service-----result--',result)
        return result[0][0]
    }
}
module.exports = new UserService();
